#ifndef _BITS_HPP_20120406
#define _BITS_HPP_20120406

#include "base.hpp"

namespace mina
{
	class ListBuffer;

	namespace bits 
	{
		enum
		{
			LOCAL_COPY   = 0,
			SOCKET_COPY  = 1,
		};

		struct Endian
		{
			public :
				static int32_t endian() { return obj.c; }

				enum { big_endian = 0, little_endian = 1 };

				static const byte * name[]; 

			private :
				union Obj 
				{   
					int16_t s;
					byte  c;  
					Obj() { s = (int16_t)1; }
				};  
				static Obj obj;
		};


		template <typename _T>
			char byte_at(uint8_t i, _T val);

		template <typename _T>
			void copy_out_b(ListBuffer & buffer, _T & val);

		template <typename _T>
			void copy_out_l(ListBuffer & buffer, _T & val);

		template <typename _T>
			void copy_out(ListBuffer & buffer, _T & val)
			{
				((Endian::endian() == Endian::big_endian)? copy_out_b<_T>(buffer, val): copy_out_l<_T>(buffer, val));
			}

		template <typename _T>
			void copy_in_b(ListBuffer & buffer, const _T & val);
		
		template <typename _T>
			void copy_in_l(ListBuffer & buffer, const _T & val);

		/// base type 
		template <typename _T>
			void copy_in(ListBuffer & buffer, const _T & val)
			{
				if (Endian::endian() == Endian::big_endian)
				{
					copy_in_b<_T>(buffer, val);
				}
				else
				{
					copy_in_l<_T>(buffer, val);
				}
			}
	}
}

#endif

